package com.qianxx.base.loading;
import android.app.Activity;
import android.view.View;

import com.qianxx.draw.R;
import com.qianxx.base.request.Config;
import com.qianxx.base.utils.LogUtil;
/**
 * 
 * @author you.y
 */
public class LoadingControl {

	private String className = "LoadingControl";
	private Activity mContext;
	public View layLoading;
	public View layLoadingHeader; // 覆盖Title的区域
	public LoadingView loadingView; // loading动画

	public LoadingControl(Activity mContext) {
		this.mContext = mContext;
		this.className = mContext.getClass().getName();
		findLoadingView(); // 查找相关控件
	}

	/**
	 * 显示loading指示
	 */
	public void showLoading(Config config) {
		setLoading(true, config);
	}

	/**
	 * 隐藏loading指示
	 */
	public void hideLoading() {
		setLoading(false, null);
	}

	private void setLoading(boolean showLoading, Config config) {
		if (layLoading == null) {
			LogUtil.w(className + " --- 没有找到layLoadingHeader");
			return;
		}
		if (showLoading) {
			layLoadingHeader.setClickable(config.isFullScreen()); // isFullScreen=true时, title部分不可点击
			layLoading.setVisibility(View.VISIBLE);
			loadingView.startLoading();
		} else {
			layLoading.setVisibility(View.GONE);
			loadingView.stopLoading();
		}
	}

	/**
	 * 查找控件
	 */
	private void findLoadingView() {
		if (mContext == null) {
			LogUtil.e("LoadingControl --- mContext为空！");
			return;
		}

		try {
			layLoading = mContext.findViewById(R.id.layLoading);
		} catch (Exception e) {
			LogUtil.w(className + " --- 没有找到layLoading");
		}
		try {
			layLoadingHeader = mContext.findViewById(R.id.layLoadingHeader);
		} catch (Exception e) {
			LogUtil.w(className + " --- 没有找到layLoadingHeader");
		}
		try {
			loadingView = (LoadingView) mContext.findViewById(R.id.loadingView);
		} catch (Exception e) {
			LogUtil.w(className + " --- 没有找到loadingView 或者 类型转换异常");
		}
	}

}
